001    /*
002     * The JastAdd Extensible Java Compiler (http://jastadd.org) is covered
003     * by the modified BSD License. You should have received a copy of the
004     * modified BSD license with this compiler.
005     * 
006     * Copyright (c) 2005-2008, Torbjorn Ekman
007     * All rights reserved.
008     */
009    
010    aspect ConstantPoolNames {
011    
012      syn lazy String TypeDecl.typeDescriptor() {
013        throw new Error("Can not compute typeDescriptor for " + getClass().getName());
014      }
015      eq ClassDecl.typeDescriptor() = "L" + constantPoolName() + ";";
016      eq InterfaceDecl.typeDescriptor() = "L" + constantPoolName() + ";";
017      eq BooleanType.typeDescriptor() = "Z";
018      eq    ByteType.typeDescriptor() = "B";
019      eq   ShortType.typeDescriptor() = "S";
020      eq     IntType.typeDescriptor() = "I";
021      eq    LongType.typeDescriptor() = "J";
022      eq    CharType.typeDescriptor() = "C";
023      eq   FloatType.typeDescriptor() = "F";
024      eq  DoubleType.typeDescriptor() = "D";
025      eq    VoidType.typeDescriptor() = "V";
026      eq   ArrayDecl.typeDescriptor() { 
027        StringBuffer dim = new StringBuffer();
028        for(int i = 0; i < dimension(); i++)
029          dim.append("[");
030        return dim.toString() + elementType().typeDescriptor(); 
031      }
032      eq UnknownType.typeDescriptor() { throw new Error("Trying to make a typeDescriptor() of Unknown"); }
033    
034      syn lazy String MethodDecl.descName() {
035        StringBuffer b = new StringBuffer();
036        b.append("(");
037        for (int i=0; i<getNumParameter(); i++)
038          b.append(getParameter(i).type().typeDescriptor());
039        b.append(")");
040        if(type().elementType().isUnknown()) {
041          System.out.println(getTypeAccess().dumpTree());
042          throw new Error("Error generating descName for " + signature() + ", did not expect unknown return type");
043        }
044        b.append(type().typeDescriptor());
045        return b.toString();
046      }
047    
048      syn lazy String ConstructorDecl.descName() {
049        StringBuffer b = new StringBuffer();
050        b.append("(");
051        // this$0
052        if(needsEnclosing())
053          b.append(enclosing().typeDescriptor());
054        if(needsSuperEnclosing())
055          b.append(superEnclosing().typeDescriptor());
056        // args
057        for (int i=0; i<getNumParameter(); i++)
058          b.append(getParameter(i).type().typeDescriptor());
059        b.append(")V");
060        return b.toString();
061      }
062    
063    
064      inh lazy String TypeDecl.destinationPath();
065      eq CompilationUnit.getTypeDecl(int index).destinationPath() = destinationPath();
066      eq CompilationUnit.getImportDecl(int index).destinationPath() = destinationPath();
067    
068      syn lazy String CompilationUnit.destinationPath() {
069        if(options().hasValueForOption("-d")) {
070          return options().getValueForOption("-d");
071        }
072        else {
073          if(fromSource()) {
074            // /home/torbjorn/sandbox/JavaCompiler/JCK/javasoft/sqe/tests/lang/icls045/icls04591m11/icls04591m11_c.java
075            // package javasoft.sqe.tests.lang.icls045.icls04591m11_p class icls04591m11_c
076            // /home/torbjorn/sandbox/JavaCompiler/JCK/javasoft/sqe/tests/lang/icls045/icls04591m11/icls04591m11.java
077            // package javasoft.sqe.tests.lang.icls045.icls04591m11 class icls04591m11
078    
079            // ta paketnamnet p och om det har fler �n 3 delar s� kontrollera om det finns som substring i pathen
080            // anv�nd i s� fall den delen av pathen
081            // forts�tt genom att ta bort sista delen i paketnamnet och g�r om proceduren
082            // sluta n�r namnet �r mindre �r 3 eller mindre
083            
084            String sourceName = pathName(); //relativeName();         // ex AST/Defines_AST_hello
085            // extract source path including package directories
086            String sourcePath = null;
087            if(sourceName.lastIndexOf(java.io.File.separator) == -1)
088              sourcePath = ".";
089            else
090              sourcePath = sourceName.substring(0, sourceName.lastIndexOf(java.io.File.separator));
091            String sourcePathPattern = sourcePath.replace(java.io.File.separatorChar, '.');
092            String[] parts = packageName().split("\\.");
093            int num = parts.length;
094            while(num > 3) {
095              StringBuffer packagePattern = new StringBuffer();
096              for(int i = 0; i < num; i++) {
097                if(i != 0) packagePattern.append(".");
098                packagePattern.append(parts[i]);
099              }
100              int index = sourcePathPattern.lastIndexOf(packagePattern.toString());
101              if(index > 0) {
102                return sourcePath.substring(0, index-1);
103              }
104              num--;
105            }
106              
107            //System.err.println("SourcePath: " + sourcePath);
108            //String[] parts = packageName().split("\\.");
109            int k = parts.length - 1;
110            while(k >= 0 && !sourcePath.endsWith(parts[k])) {
111              //System.err.println(sourcePath + " does not end with " + parts[k]);
112              k--;
113            }
114            if(k >= 0) {
115              for(int i = k; i >= 0; i--) {
116                sourcePath = sourcePath.substring(0, sourcePath.lastIndexOf(parts[i]));
117                //System.err.println("new candidate is " + sourcePath);
118              }
119            }
120            if(sourcePath.equals(""))
121              sourcePath = ".";
122            //System.err.println("SourcePath after: " + sourcePath);
123            return sourcePath;
124            /*
125            // extract first part of package name
126            String prefix;
127            int pos = packageName().indexOf('.');       // AST
128            if(pos != -1)
129              prefix = packageName().substring(0, pos-1);
130            else
131              prefix = packageName();
132            // add separator
133            prefix = prefix + java.io.File.separator;
134            // find last occurance
135            pos = sourceName.lastIndexOf(prefix);
136            if(pos > 0 && !packageName().equals(""))
137              return sourceName.substring(0, pos-1);
138            */
139          }
140          if(pathName != null)
141            return pathName;
142          else
143            return ".";
144        }
145      }
146    }