Search in sources :

Example 11 with CompilationUnitDeclaration

use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.

the class CheASTParser method internalCreateAST.

private ASTNode internalCreateAST(IProgressMonitor monitor) {
    boolean needToResolveBindings = (this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0;
    switch(this.astKind) {
        case K_CLASS_BODY_DECLARATIONS:
        case K_EXPRESSION:
        case K_STATEMENTS:
            if (this.rawSource == null) {
                if (this.typeRoot != null) {
                    // get the source from the type root
                    if (this.typeRoot instanceof ICompilationUnit) {
                        org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
                        this.rawSource = sourceUnit.getContents();
                    } else if (this.typeRoot instanceof IClassFile) {
                        try {
                            String sourceString = this.typeRoot.getSource();
                            if (sourceString != null) {
                                this.rawSource = sourceString.toCharArray();
                            }
                        } catch (JavaModelException e) {
                            // an error occured accessing the java element
                            StringWriter stringWriter = new StringWriter();
                            PrintWriter writer = null;
                            try {
                                writer = new PrintWriter(stringWriter);
                                e.printStackTrace(writer);
                            } finally {
                                if (writer != null)
                                    writer.close();
                            }
                            throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
                        }
                    }
                }
            }
            if (this.rawSource != null) {
                if (this.sourceOffset + this.sourceLength > this.rawSource.length) {
                    throw new IllegalStateException();
                }
                return internalCreateASTForKind();
            }
            break;
        case K_COMPILATION_UNIT:
            CompilationUnitDeclaration compilationUnitDeclaration = null;
            try {
                NodeSearcher searcher = null;
                org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null;
                WorkingCopyOwner wcOwner = this.workingCopyOwner;
                if (this.typeRoot instanceof ICompilationUnit) {
                    /*
							 * this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements
							 * both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit
							 */
                    sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
                    /*
							 * use a BasicCompilation that caches the source instead of using the compilationUnitSource directly
							 * (if it is a working copy, the source can change between the parse and the AST convertion)
							 * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632)
							 */
                    sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project);
                    wcOwner = ((ICompilationUnit) this.typeRoot).getOwner();
                } else if (this.typeRoot instanceof IClassFile) {
                    try {
                        String sourceString = this.typeRoot.getSource();
                        if (sourceString == null) {
                            throw new IllegalStateException();
                        }
                        PackageFragment packageFragment = (PackageFragment) this.typeRoot.getParent();
                        BinaryType type = (BinaryType) this.typeRoot.findPrimaryType();
                        IBinaryType binaryType = (IBinaryType) type.getElementInfo();
                        // file name is used to recreate the Java element, so it has to be the toplevel .class file name
                        char[] fileName = binaryType.getFileName();
                        int firstDollar = CharOperation.indexOf('$', fileName);
                        if (firstDollar != -1) {
                            char[] suffix = SuffixConstants.SUFFIX_class;
                            int suffixLength = suffix.length;
                            char[] newFileName = new char[firstDollar + suffixLength];
                            System.arraycopy(fileName, 0, newFileName, 0, firstDollar);
                            System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength);
                            fileName = newFileName;
                        }
                        sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project);
                    } catch (JavaModelException e) {
                        // an error occured accessing the java element
                        StringWriter stringWriter = new StringWriter();
                        PrintWriter writer = null;
                        try {
                            writer = new PrintWriter(stringWriter);
                            e.printStackTrace(writer);
                        } finally {
                            if (writer != null)
                                writer.close();
                        }
                        throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
                    }
                } else if (this.rawSource != null) {
                    needToResolveBindings = ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0) && this.unitName != null && (this.project != null || this.classpaths != null || this.sourcepaths != null || ((this.bits & CompilationUnitResolver.INCLUDE_RUNNING_VM_BOOTCLASSPATH) != 0)) && this.compilerOptions != null;
                    //$NON-NLS-1$
                    sourceUnit = new BasicCompilationUnit(this.rawSource, null, this.unitName == null ? "" : this.unitName, this.project);
                } else {
                    throw new IllegalStateException();
                }
                if ((this.bits & CompilationUnitResolver.PARTIAL) != 0) {
                    searcher = new NodeSearcher(this.focalPointPosition);
                }
                int flags = 0;
                if ((this.bits & CompilationUnitResolver.STATEMENT_RECOVERY) != 0) {
                    flags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
                }
                if (searcher == null && ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0)) {
                    flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
                }
                if (needToResolveBindings) {
                    if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
                        flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
                    }
                    try {
                        // parse and resolve
                        compilationUnitDeclaration = CheCompilationUnitResolver.resolve(sourceUnit, this.project, getClasspath(), searcher, this.compilerOptions, this.workingCopyOwner, flags, monitor);
                    } catch (JavaModelException e) {
                        flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
                        compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
                        needToResolveBindings = false;
                    }
                } else {
                    compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
                    needToResolveBindings = false;
                }
                CompilationUnit result = CompilationUnitResolver.convert(compilationUnitDeclaration, sourceUnit.getContents(), this.apiLevel, this.compilerOptions, needToResolveBindings, wcOwner, needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, flags, monitor, this.project != null);
                result.setTypeRoot(this.typeRoot);
                return result;
            } finally {
                if (compilationUnitDeclaration != null && ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0)) {
                    compilationUnitDeclaration.cleanUp();
                }
            }
    }
    throw new IllegalStateException();
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) StringWriter(java.io.StringWriter) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) DefaultWorkingCopyOwner(org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner) PrintWriter(java.io.PrintWriter) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) BinaryType(org.eclipse.jdt.internal.core.BinaryType) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit)

Example 12 with CompilationUnitDeclaration

use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project che by eclipse.

the class ReconcileWorkingCopyOperation method makeConsistent.

/*
     * Makes the given working copy consistent, computes the delta and computes an AST if needed.
     * Returns the AST.
     */
public org.eclipse.jdt.core.dom.CompilationUnit makeConsistent(CompilationUnit workingCopy) throws JavaModelException {
    if (!workingCopy.isConsistent()) {
        // make working copy consistent
        if (this.problems == null)
            this.problems = new HashMap();
        this.resolveBindings = this.requestorIsActive;
        this.ast = workingCopy.makeConsistent(this.astLevel, this.resolveBindings, this.reconcileFlags, this.problems, this.progressMonitor);
        this.deltaBuilder.buildDeltas();
        if (this.ast != null && this.deltaBuilder.delta != null)
            this.deltaBuilder.delta.changedAST(this.ast);
        return this.ast;
    }
    if (this.ast != null)
        // no need to recompute AST if known already
        return this.ast;
    CompilationUnitDeclaration unit = null;
    try {
        JavaModelManager.getJavaModelManager().abortOnMissingSource.set(Boolean.TRUE);
        CompilationUnit source = workingCopy.cloneCachingContents();
        // find problems if needed
        if (JavaProject.hasJavaNature(workingCopy.getJavaProject().getProject()) && (this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0) {
            this.resolveBindings = this.requestorIsActive;
            if (this.problems == null)
                this.problems = new HashMap();
            unit = CompilationUnitProblemFinder.process(source, this.workingCopyOwner, this.problems, this.astLevel != ICompilationUnit.NO_AST, /*creating AST if level is not NO_AST */
            this.reconcileFlags, this.progressMonitor);
            if (this.progressMonitor != null)
                this.progressMonitor.worked(1);
        }
        // create AST if needed
        if (this.astLevel != ICompilationUnit.NO_AST && unit != null) /*unit is null if working copy is consistent && (problem detection not forced || non-Java project) -> don't create
                    AST as per API*/
        {
            Map options = workingCopy.getJavaProject().getOptions(true);
            // convert AST
            this.ast = CheAST.convertCompilationUnit(this.astLevel, unit, options, this.resolveBindings, source, this.reconcileFlags, this.progressMonitor);
            if (this.ast != null) {
                if (this.deltaBuilder.delta == null) {
                    this.deltaBuilder.delta = new JavaElementDelta(workingCopy);
                }
                this.deltaBuilder.delta.changedAST(this.ast);
            }
            if (this.progressMonitor != null)
                this.progressMonitor.worked(1);
        }
    } catch (JavaModelException e) {
        if (JavaProject.hasJavaNature(workingCopy.getJavaProject().getProject()))
            throw e;
    // else JavaProject has lost its nature (or most likely was closed/deleted) while reconciling -> ignore
    // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=100919)
    } finally {
        JavaModelManager.getJavaModelManager().abortOnMissingSource.set(null);
        if (unit != null) {
            unit.cleanUp();
        }
    }
    return this.ast;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with CompilationUnitDeclaration

use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project lombok by rzwitserloot.

the class DebugSnapshot method ownerName.

private String ownerName() {
    CompilationUnitDeclaration node = owner.get();
    if (node == null)
        return "--GCed--";
    char[] tn = node.getMainTypeName();
    char[] fs = node.getFileName();
    if (tn == null || tn.length == 0) {
        return (fs == null || fs.length == 0) ? "--UNKNOWN--" : new String(fs);
    }
    return new String(tn);
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)

Example 14 with CompilationUnitDeclaration

use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project lombok by rzwitserloot.

the class PatchDelegate method generateDelegateMethods.

/*
	 * We may someday finish this method. Steps to be completed:
	 * 
	 * (A) Turn any Parameterized anythings into non-parameterized versions. Resolving parameterized stuff will definitely not work safely.
	 * (B) scope.problemReporter() will need to return a noop reporter as various errors are marked off.
	 * (C) Find a way to do _something_ for references to typevars (i.e. 'T') which are declared on the method itself.
	 * (D) getTypeBinding isn't public, so call it via reflection.
	 */
//	private static TypeBinding safeResolveAndErase(TypeReference ref, Scope scope) {
//		if (ref.resolvedType != null) {
//			return ref.resolvedType.isValidBinding() ? ref.resolvedType : null;
//		}
//		
//		try {
//			TypeBinding bind = ref.getTypeBinding(scope);
//			if (!bind.isValidBinding()) return null;
//		} catch (AbortCompilation e) {
//			return null;
//		}
//		return bind.erasure();
//	}
/*
	 * Not using this because calling clone.resolveType() taints a bunch of caches and reports erroneous errors.
	 */
//	private static void removeExistingMethods(List<BindingTuple> list, TypeDeclaration decl, ClassScope scope) {
//		for (AbstractMethodDeclaration methodDecl : decl.methods) {
//			if (!(methodDecl instanceof MethodDeclaration)) continue;
//			MethodDeclaration md = (MethodDeclaration) methodDecl;
//			char[] name = md.selector;
//			TypeBinding[] args = md.arguments == null ? new TypeBinding[0] : new TypeBinding[md.arguments.length];
//			for (int i = 0; i < args.length; i++) {
//				TypeReference clone = Eclipse.copyType(md.arguments[i].type, md.arguments[i]);
//				args[i] = clone.resolveType(scope).erasure(); // This is the problematic line
//			}
//			Iterator<BindingTuple> it = list.iterator();
//			methods:
//			while (it.hasNext()) {
//				MethodBinding mb = it.next().parameterized;
//				if (!Arrays.equals(mb.selector, name)) continue;
//				int paramLen = mb.parameters == null ? 0 : mb.parameters.length;
//				if (paramLen != args.length) continue;
//				if (md.typeParameters == null || md.typeParameters.length == 0) {
//					for (int i = 0; i < paramLen; i++) {
//						if (!mb.parameters[i].erasure().isEquivalentTo(args[i])) continue methods;
//					}
//				} else {
//					for (int i = 0; i < paramLen; i++) {
//						if (!mb.parameters[i].erasure().isEquivalentTo(args[i])) ;
//					}
//					//BUG #???: We erase the method's parameter types using  the class scope, but we should be using the method scope.
//					// In practice this is no problem UNLESS the method has type parameters, such as <T> T[] toArray(T[] in).
//					// In this case the class scope cannot resolve the T[] parameter and erase it to Object[], which is a big problem because
//					// it would mean manually writing <X> X[] toArray(X[] in) does NOT stop lombok from ALSO trying to make the delegated toArray method,
//					// thus causing an error (2 methods with post-erasure duplicate signatures). Our 'fix' for this is to treat any method with type parameters
//					// as if each parameter's type matches anything else; so, if the name matches and the parameter count, we DONT generate it, even if its just
//					// an overloaded method.
//					//
//					// The reason we do this now is because making that MethodScope properly is effectively impossible at this step, so we need to do the resolving
//					// ourselves, which involves chasing down array bindings (T[]), following the path down type variables, i.e. <X extends Y, Y extends T>, and then
//					// resolving the final result of this exercise against the class scope.
//					
//					// When this crappy incomplete workaround of ours occurs, we end up in this else block, which does nothing and thus we fall through and remove
//					// the method.
//				}
//				it.remove(); // Method already exists in this class - don't create a delegating implementation.
//			}
//		}
//	}
private static void generateDelegateMethods(EclipseNode typeNode, List<BindingTuple> methods, DelegateReceiver delegateReceiver) {
    CompilationUnitDeclaration top = (CompilationUnitDeclaration) typeNode.top().get();
    for (BindingTuple pair : methods) {
        EclipseNode annNode = typeNode.getAst().get(pair.responsible);
        MethodDeclaration method = createDelegateMethod(pair.fieldName, typeNode, pair, top.compilationResult, annNode, delegateReceiver);
        if (method != null) {
            SetGeneratedByVisitor visitor = new SetGeneratedByVisitor(annNode.get());
            method.traverse(visitor, ((TypeDeclaration) typeNode.get()).scope);
            injectMethod(typeNode, method);
        }
    }
}
Also used : SetGeneratedByVisitor(lombok.eclipse.handlers.SetGeneratedByVisitor) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) EclipseNode(lombok.eclipse.EclipseNode)

Example 15 with CompilationUnitDeclaration

use of org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration in project lombok by rzwitserloot.

the class PatchDelegate method handleDelegateForType.

public static boolean handleDelegateForType(ClassScope scope) {
    if (TransformEclipseAST.disableLombok)
        return false;
    if (!hasDelegateMarkedFieldsOrMethods(scope.referenceContext))
        return false;
    List<ClassScopeEntry> stack = visited.get();
    StringBuilder corrupted = null;
    for (ClassScopeEntry entry : stack) {
        if (corrupted != null) {
            corrupted.append(" -> ").append(nameOfScope(entry.scope));
        } else if (entry.scope == scope) {
            corrupted = new StringBuilder().append(nameOfScope(scope));
        }
    }
    if (corrupted != null) {
        boolean found = false;
        String path = corrupted.toString();
        for (ClassScopeEntry entry : stack) {
            if (!found && entry.scope == scope)
                found = true;
            if (found)
                entry.corruptedPath = path;
        }
    } else {
        ClassScopeEntry entry = new ClassScopeEntry(scope);
        stack.add(entry);
        try {
            TypeDeclaration decl = scope.referenceContext;
            if (decl != null) {
                CompilationUnitDeclaration cud = scope.compilationUnitScope().referenceContext;
                EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                List<BindingTuple> methodsToDelegate = new ArrayList<BindingTuple>();
                fillMethodBindingsForFields(cud, scope, methodsToDelegate);
                if (entry.corruptedPath != null) {
                    eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath);
                } else {
                    generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.FIELD);
                }
                methodsToDelegate.clear();
                fillMethodBindingsForMethods(cud, scope, methodsToDelegate);
                if (entry.corruptedPath != null) {
                    eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath);
                } else {
                    generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.METHOD);
                }
            }
        } finally {
            stack.remove(stack.size() - 1);
        }
    }
    return false;
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) ArrayList(java.util.ArrayList) EclipseAST(lombok.eclipse.EclipseAST) TransformEclipseAST(lombok.eclipse.TransformEclipseAST) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)17 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)4 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)4 CategorizedProblem (org.eclipse.jdt.core.compiler.CategorizedProblem)3 Compiler (org.eclipse.jdt.internal.compiler.Compiler)3 ICompilerRequestor (org.eclipse.jdt.internal.compiler.ICompilerRequestor)3 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)3 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)3 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)3 ArrayList (java.util.ArrayList)2 EclipseAST (lombok.eclipse.EclipseAST)2 EclipseNode (lombok.eclipse.EclipseNode)2 TransformEclipseAST (lombok.eclipse.TransformEclipseAST)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 IErrorHandlingPolicy (org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy)2 IProblemFactory (org.eclipse.jdt.internal.compiler.IProblemFactory)2 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)2 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)2 Parser (org.eclipse.jdt.internal.compiler.parser.Parser)2