A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / SourceTypeConverter.java
index e32922d..4d6d794 100644 (file)
@@ -12,15 +12,14 @@ package net.sourceforge.phpdt.internal.compiler.parser;
 
 /**
  * Converter from source element type to parsed compilation unit.
- *
- * Limitation:
- * | The source element field does not carry any information for its constant part, thus
- * | the converted parse tree will not include any field initializations.
- * | Therefore, any binary produced by compiling against converted source elements will
- * | not take advantage of remote field constant inlining.
- * | Given the intended purpose of the conversion is to resolve references, this is not
- * | a problem.
- *
+ * 
+ * Limitation: | The source element field does not carry any information for its
+ * constant part, thus | the converted parse tree will not include any field
+ * initializations. | Therefore, any binary produced by compiling against
+ * converted source elements will | not take advantage of remote field constant
+ * inlining. | Given the intended purpose of the conversion is to resolve
+ * references, this is not | a problem.
+ * 
  */
 
 import java.util.ArrayList;
@@ -48,54 +47,51 @@ import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
 
 public class SourceTypeConverter implements CompilerModifiers {
-       
+
        private boolean needFieldInitialization;
+
        private CompilationUnitDeclaration unit;
+
        private UnitParser parser;
+
        private ProblemReporter problemReporter;
-       
-       private SourceTypeConverter(boolean needFieldInitialization, ProblemReporter problemReporter) {
+
+       private SourceTypeConverter(boolean needFieldInitialization,
+                       ProblemReporter problemReporter) {
                this.needFieldInitialization = needFieldInitialization;
                this.problemReporter = problemReporter;
        }
 
        /*
-        * Convert a set of source element types into a parsed compilation unit declaration
-        * The argument types are then all grouped in the same unit. The argument types must 
-        * at least contain one type.
-        * Can optionally ignore fields & methods or member types or field initialization
+        * Convert a set of source element types into a parsed compilation unit
+        * declaration The argument types are then all grouped in the same unit. The
+        * argument types must at least contain one type. Can optionally ignore
+        * fields & methods or member types or field initialization
         */
        public static CompilationUnitDeclaration buildCompilationUnit(
-               ISourceType[] sourceTypes,
-               boolean needFieldsAndMethods,
-               boolean needMemberTypes,
-               boolean needFieldInitialization,
-               ProblemReporter problemReporter,
-               CompilationResult compilationResult) {
-                       
-               return 
-                       new SourceTypeConverter(needFieldInitialization, problemReporter).convert(
-                               sourceTypes, 
-                               needFieldsAndMethods,
-                               needMemberTypes,
-                               compilationResult);
+                       ISourceType[] sourceTypes, boolean needFieldsAndMethods,
+                       boolean needMemberTypes, boolean needFieldInitialization,
+                       ProblemReporter problemReporter, CompilationResult compilationResult) {
+
+               return new SourceTypeConverter(needFieldInitialization, problemReporter)
+                               .convert(sourceTypes, needFieldsAndMethods, needMemberTypes,
+                                               compilationResult);
        }
 
        /*
-        * Convert a set of source element types into a parsed compilation unit declaration
-        * The argument types are then all grouped in the same unit. The argument types must 
-        * at least contain one type.
+        * Convert a set of source element types into a parsed compilation unit
+        * declaration The argument types are then all grouped in the same unit. The
+        * argument types must at least contain one type.
         */
-       private CompilationUnitDeclaration convert(
-               ISourceType[] sourceTypes,
-               boolean needFieldsAndMethods,
-               boolean needMemberTypes,
-               CompilationResult compilationResult) {
+       private CompilationUnitDeclaration convert(ISourceType[] sourceTypes,
+                       boolean needFieldsAndMethods, boolean needMemberTypes,
+                       CompilationResult compilationResult) {
                ISourceType sourceType = sourceTypes[0];
                if (sourceType.getName() == null)
                        return null; // do a basic test that the sourceType is valid
 
-               this.unit = new CompilationUnitDeclaration(problemReporter, compilationResult, 0);
+               this.unit = new CompilationUnitDeclaration(problemReporter,
+                               compilationResult, 0);
                // not filled at this point
 
                /* only positions available */
@@ -103,32 +99,35 @@ public class SourceTypeConverter implements CompilerModifiers {
                int end = sourceType.getNameSourceEnd();
 
                /* convert package and imports */
-//             if (sourceType.getPackageName() != null
-//                     && sourceType.getPackageName().length > 0)
-//                     // if its null then it is defined in the default package
-//                     this.unit.currentPackage =
-//                             createImportReference(sourceType.getPackageName(), start, end);
+               // if (sourceType.getPackageName() != null
+               // && sourceType.getPackageName().length > 0)
+               // // if its null then it is defined in the default package
+               // this.unit.currentPackage =
+               // createImportReference(sourceType.getPackageName(), start, end);
                char[][] importNames = sourceType.getImports();
                int importCount = importNames == null ? 0 : importNames.length;
                this.unit.imports = new ImportReference[importCount];
-//             for (int i = 0; i < importCount; i++)
-//                     this.unit.imports[i] = createImportReference(importNames[i], start, end);
+               // for (int i = 0; i < importCount; i++)
+               // this.unit.imports[i] = createImportReference(importNames[i], start,
+               // end);
                /* convert type(s) */
                int typeCount = sourceTypes.length;
                this.unit.types = new ArrayList(typeCount);
                for (int i = 0; i < typeCount; i++) {
-                       this.unit.types.set(i,
-                                                       convert(sourceTypes[i], needFieldsAndMethods, needMemberTypes, compilationResult));
-//                     this.unit.types[i] =
-//                             convert(sourceTypes[i], needFieldsAndMethods, needMemberTypes, compilationResult);
+                       this.unit.types.set(i, convert(sourceTypes[i],
+                                       needFieldsAndMethods, needMemberTypes, compilationResult));
+                       // this.unit.types[i] =
+                       // convert(sourceTypes[i], needFieldsAndMethods, needMemberTypes,
+                       // compilationResult);
                }
                return this.unit;
        }
-       
+
        /*
         * Convert a field source element into a parsed field declaration
         */
-       private FieldDeclaration convert(ISourceField sourceField, TypeDeclaration type) {
+       private FieldDeclaration convert(ISourceField sourceField,
+                       TypeDeclaration type) {
 
                FieldDeclaration field = new FieldDeclaration();
 
@@ -148,23 +147,24 @@ public class SourceTypeConverter implements CompilerModifiers {
                        char[] initializationSource = sourceField.getInitializationSource();
                        if (initializationSource != null) {
                                if (this.parser == null) {
-                                       this.parser = 
-                                               new UnitParser(
-                                                       this.problemReporter); 
-//                                                     true, 
-//                                                     this.problemReporter.options.sourceLevel >= CompilerOptions.JDK1_4);
+                                       this.parser = new UnitParser(this.problemReporter);
+                                       // true,
+                                       // this.problemReporter.options.sourceLevel >=
+                                       // CompilerOptions.JDK1_4);
                                }
                                this.parser.parse(field, type, this.unit, initializationSource);
                        }
                }
-               
+
                return field;
        }
 
        /*
-        * Convert a method source element into a parsed method/constructor declaration 
+        * Convert a method source element into a parsed method/constructor
+        * declaration
         */
-       private AbstractMethodDeclaration convert(ISourceMethod sourceMethod, CompilationResult compilationResult) {
+       private AbstractMethodDeclaration convert(ISourceMethod sourceMethod,
+                       CompilationResult compilationResult) {
 
                AbstractMethodDeclaration method;
 
@@ -173,34 +173,34 @@ public class SourceTypeConverter implements CompilerModifiers {
                int end = sourceMethod.getNameSourceEnd();
 
                if (sourceMethod.isConstructor()) {
-                       ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
+                       ConstructorDeclaration decl = new ConstructorDeclaration(
+                                       compilationResult);
                        decl.isDefaultConstructor = false;
                        method = decl;
                } else {
                        MethodDeclaration decl = new MethodDeclaration(compilationResult);
                        /* convert return type */
-                       decl.returnType =
-                               createTypeReference(sourceMethod.getReturnTypeName(), start, end);
+                       decl.returnType = createTypeReference(sourceMethod
+                                       .getReturnTypeName(), start, end);
                        method = decl;
                }
                method.selector = sourceMethod.getSelector();
                method.modifiers = sourceMethod.getModifiers();
                method.sourceStart = start;
                method.sourceEnd = end;
-               method.declarationSourceStart = sourceMethod.getDeclarationSourceStart();
+               method.declarationSourceStart = sourceMethod
+                               .getDeclarationSourceStart();
                method.declarationSourceEnd = sourceMethod.getDeclarationSourceEnd();
 
                /* convert arguments */
                char[][] argumentTypeNames = sourceMethod.getArgumentTypeNames();
                char[][] argumentNames = sourceMethod.getArgumentNames();
-               int argumentCount = argumentTypeNames == null ? 0 : argumentTypeNames.length;
+               int argumentCount = argumentTypeNames == null ? 0
+                               : argumentTypeNames.length;
                long position = (long) start << 32 + end;
                method.arguments = new Argument[argumentCount];
                for (int i = 0; i < argumentCount; i++) {
-                       method.arguments[i] =
-                               new Argument(
-                                       argumentNames[i],
-                                       position,
+                       method.arguments[i] = new Argument(argumentNames[i], position,
                                        createTypeReference(argumentTypeNames[i], start, end),
                                        AccDefault);
                        // do not care whether was final or not
@@ -208,25 +208,24 @@ public class SourceTypeConverter implements CompilerModifiers {
 
                /* convert thrown exceptions */
                char[][] exceptionTypeNames = sourceMethod.getExceptionTypeNames();
-               int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
+               int exceptionCount = exceptionTypeNames == null ? 0
+                               : exceptionTypeNames.length;
                method.thrownExceptions = new TypeReference[exceptionCount];
                for (int i = 0; i < exceptionCount; i++) {
-                       method.thrownExceptions[i] =
-                               createTypeReference(exceptionTypeNames[i], start, end);
+                       method.thrownExceptions[i] = createTypeReference(
+                                       exceptionTypeNames[i], start, end);
                }
                return method;
        }
 
        /*
         * Convert a source element type into a parsed type declaration
-        *
+        * 
         * Can optionally ignore fields & methods
         */
-       private TypeDeclaration convert(
-               ISourceType sourceType,
-               boolean needFieldsAndMethods,
-               boolean needMemberTypes,
-               CompilationResult compilationResult) {
+       private TypeDeclaration convert(ISourceType sourceType,
+                       boolean needFieldsAndMethods, boolean needMemberTypes,
+                       CompilationResult compilationResult) {
                /* create type declaration - can be member type */
                TypeDeclaration type;
                if (sourceType.getEnclosingType() == null) {
@@ -245,25 +244,24 @@ public class SourceTypeConverter implements CompilerModifiers {
 
                /* set superclass and superinterfaces */
                if (sourceType.getSuperclassName() != null)
-                       type.superclass =
-                               createTypeReference(sourceType.getSuperclassName(), start, end);
+                       type.superclass = createTypeReference(sourceType
+                                       .getSuperclassName(), start, end);
                char[][] interfaceNames = sourceType.getInterfaceNames();
                int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
                type.superInterfaces = new TypeReference[interfaceCount];
                for (int i = 0; i < interfaceCount; i++) {
-                       type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end);
+                       type.superInterfaces[i] = createTypeReference(interfaceNames[i],
+                                       start, end);
                }
                /* convert member types */
                if (needMemberTypes) {
                        ISourceType[] sourceMemberTypes = sourceType.getMemberTypes();
-                       int sourceMemberTypeCount =
-                               sourceMemberTypes == null ? 0 : sourceMemberTypes.length;
+                       int sourceMemberTypeCount = sourceMemberTypes == null ? 0
+                                       : sourceMemberTypes.length;
                        type.memberTypes = new MemberTypeDeclaration[sourceMemberTypeCount];
                        for (int i = 0; i < sourceMemberTypeCount; i++) {
-                               type.memberTypes[i] =
-                                       (MemberTypeDeclaration) convert(sourceMemberTypes[i],
-                                               needFieldsAndMethods,
-                                               true,
+                               type.memberTypes[i] = (MemberTypeDeclaration) convert(
+                                               sourceMemberTypes[i], needFieldsAndMethods, true,
                                                compilationResult);
                        }
                }
@@ -271,7 +269,8 @@ public class SourceTypeConverter implements CompilerModifiers {
                if (needFieldsAndMethods) {
                        /* convert fields */
                        ISourceField[] sourceFields = sourceType.getFields();
-                       int sourceFieldCount = sourceFields == null ? 0 : sourceFields.length;
+                       int sourceFieldCount = sourceFields == null ? 0
+                                       : sourceFields.length;
                        type.fields = new FieldDeclaration[sourceFieldCount];
                        for (int i = 0; i < sourceFieldCount; i++) {
                                type.fields[i] = convert(sourceFields[i], type);
@@ -279,9 +278,10 @@ public class SourceTypeConverter implements CompilerModifiers {
 
                        /* convert methods - need to add default constructor if necessary */
                        ISourceMethod[] sourceMethods = sourceType.getMethods();
-                       int sourceMethodCount = sourceMethods == null ? 0 : sourceMethods.length;
+                       int sourceMethodCount = sourceMethods == null ? 0
+                                       : sourceMethods.length;
 
-                       /* source type has a constructor ?           */
+                       /* source type has a constructor ? */
                        /* by default, we assume that one is needed. */
                        int neededCount = 0;
                        if (!type.isInterface()) {
@@ -289,19 +289,22 @@ public class SourceTypeConverter implements CompilerModifiers {
                                for (int i = 0; i < sourceMethodCount; i++) {
                                        if (sourceMethods[i].isConstructor()) {
                                                neededCount = 0;
-                                               // Does not need the extra constructor since one constructor already exists.
+                                               // Does not need the extra constructor since one
+                                               // constructor already exists.
                                                break;
                                        }
                                }
                        }
-                       type.methods = new AbstractMethodDeclaration[sourceMethodCount + neededCount];
+                       type.methods = new AbstractMethodDeclaration[sourceMethodCount
+                                       + neededCount];
                        if (neededCount != 0) { // add default constructor in first position
                                type.methods[0] = type.createsInternalConstructor(false, false);
                        }
                        boolean isInterface = type.isInterface();
                        for (int i = 0; i < sourceMethodCount; i++) {
-                               AbstractMethodDeclaration method =convert(sourceMethods[i], compilationResult);
-                               if (isInterface || method.isAbstract()) { // fix-up flag 
+                               AbstractMethodDeclaration method = convert(sourceMethods[i],
+                                               compilationResult);
+                               if (isInterface || method.isAbstract()) { // fix-up flag
                                        method.modifiers |= AccSemicolonBody;
                                }
                                type.methods[neededCount + i] = method;
@@ -313,41 +316,38 @@ public class SourceTypeConverter implements CompilerModifiers {
        /*
         * Build an import reference from an import name, e.g. java.lang.*
         */
-//     private ImportReference createImportReference(
-//             char[] importName,
-//             int start,
-//             int end) {
-//
-//             /* count identifiers */
-//             int max = importName.length;
-//             int identCount = 0;
-//             for (int i = 0; i < max; i++) {
-//                     if (importName[i] == '.')
-//                             identCount++;
-//             }
-//             /* import on demand? */
-//             boolean onDemand = importName[max - 1] == '*';
-//             if (!onDemand)
-//                     identCount++; // one more ident than dots
-//
-//             long[] positions = new long[identCount];
-//             long position = (long) start << 32 + end;
-//             for (int i = 0; i < identCount; i++) {
-//                     positions[i] = position;
-//             }
-//             return new ImportReference(
-//                     CharOperation.splitOn('.', importName, 0, max - (onDemand ? 2 : 0)),
-//                     positions,
-//                     onDemand);
-//     }
-
+       // private ImportReference createImportReference(
+       // char[] importName,
+       // int start,
+       // int end) {
+       //
+       // /* count identifiers */
+       // int max = importName.length;
+       // int identCount = 0;
+       // for (int i = 0; i < max; i++) {
+       // if (importName[i] == '.')
+       // identCount++;
+       // }
+       // /* import on demand? */
+       // boolean onDemand = importName[max - 1] == '*';
+       // if (!onDemand)
+       // identCount++; // one more ident than dots
+       //
+       // long[] positions = new long[identCount];
+       // long position = (long) start << 32 + end;
+       // for (int i = 0; i < identCount; i++) {
+       // positions[i] = position;
+       // }
+       // return new ImportReference(
+       // CharOperation.splitOn('.', importName, 0, max - (onDemand ? 2 : 0)),
+       // positions,
+       // onDemand);
+       // }
        /*
         * Build a type reference from a readable name, e.g. java.lang.Object[][]
         */
-       private TypeReference createTypeReference(
-               char[] typeSignature,
-               int start,
-               int end) {
+       private TypeReference createTypeReference(char[] typeSignature, int start,
+                       int end) {
 
                /* count identifiers and dimensions */
                int max = typeSignature.length;
@@ -356,24 +356,26 @@ public class SourceTypeConverter implements CompilerModifiers {
                int identCount = 1;
                for (int i = 0; i < max; i++) {
                        switch (typeSignature[i]) {
-                               case '[' :
-                                       if (dim == 0)
-                                               dimStart = i;
-                                       dim++;
-                                       break;
-                               case '.' :
-                                       identCount++;
-                                       break;
+                       case '[':
+                               if (dim == 0)
+                                       dimStart = i;
+                               dim++;
+                               break;
+                       case '.':
+                               identCount++;
+                               break;
                        }
                }
                /* rebuild identifiers and dimensions */
                if (identCount == 1) { // simple type reference
                        if (dim == 0) {
-                               return new SingleTypeReference(typeSignature, (((long) start )<< 32) + end);
+                               return new SingleTypeReference(typeSignature,
+                                               (((long) start) << 32) + end);
                        } else {
                                char[] identifier = new char[dimStart];
                                System.arraycopy(typeSignature, 0, identifier, 0, dimStart);
-                               return new ArrayTypeReference(identifier, dim, (((long) start) << 32) + end);
+                               return new ArrayTypeReference(identifier, dim,
+                                               (((long) start) << 32) + end);
                        }
                } else { // qualified type reference
                        long[] positions = new long[identCount];
@@ -381,12 +383,13 @@ public class SourceTypeConverter implements CompilerModifiers {
                        for (int i = 0; i < identCount; i++) {
                                positions[i] = pos;
                        }
-                       char[][] identifiers =
-                               CharOperation.splitOn('.', typeSignature, 0, dimStart);
+                       char[][] identifiers = CharOperation.splitOn('.', typeSignature, 0,
+                                       dimStart);
                        if (dim == 0) {
                                return new QualifiedTypeReference(identifiers, positions);
                        } else {
-                               return new ArrayQualifiedTypeReference(identifiers, dim, positions);
+                               return new ArrayQualifiedTypeReference(identifiers, dim,
+                                               positions);
                        }
                }
        }