List of usage examples for org.eclipse.jdt.internal.compiler.ast Expression traverse
public void traverse(ASTVisitor visitor, ClassScope scope)
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.AbstractMethodMappingDeclaration.java
License:Open Source License
/** * Look for any single names in the given expression that refer to an argument of the base method. * For error reporting only, thus only the first occurrence is of interest. * * @param mappedArgExpr expression to investigate * @param blockScope just to please the visitor * @param arguments base arguments to match against * @return a found reference or null.//from w ww . j av a2 s . c o m */ SingleNameReference findBaseArgName(Expression mappedArgExpr, BlockScope blockScope, final Argument[] arguments) { @SuppressWarnings("serial") class FoundException extends RuntimeException { SingleNameReference name; FoundException(SingleNameReference name) { this.name = name; } } ASTVisitor visitor = new ASTVisitor() { public void endVisit(SingleNameReference nameRef, BlockScope blockScope2) { for (int i = 0; i < arguments.length; i++) { if (CharOperation.equals(arguments[i].name, nameRef.token)) throw new FoundException(nameRef); } } }; try { mappedArgExpr.traverse(visitor, blockScope); } catch (FoundException e) { return e.name; } return null; }
From source file:spoon.support.builder.JDTTreeBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w ww . j av a 2 s . co m public boolean visit(AllocationExpression allocationExpression, BlockScope scope) { CtNewClass<?> c = factory.Core().createNewClass(); if (allocationExpression.type != null) c.setType(references.getTypeReference(allocationExpression.type.resolvedType)); c.setExecutable(references.getExecutableReference(allocationExpression.binding)); c.getExecutable().setType((CtTypeReference) c.getExecutable().getDeclaringType()); context.enter(c, allocationExpression); if (allocationExpression.enclosingInstance() != null) { context.target.push(c); allocationExpression.enclosingInstance().traverse(this, scope); context.target.pop(); } context.pushArgument(c); if (allocationExpression.arguments != null) { for (Expression e : allocationExpression.arguments) e.traverse(this, scope); } context.popArgument(c); return false; }
From source file:spoon.support.builder.JDTTreeBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* w ww.j ava 2s . c o m*/ public boolean visit(ArrayAllocationExpression arrayAllocationExpression, BlockScope scope) { CtNewArray<?> array = factory.Core().createNewArray(); array.setType(references.getTypeReference(arrayAllocationExpression.resolvedType)); context.enter(array, arrayAllocationExpression); context.pushArgument(array); if (arrayAllocationExpression.dimensions != null) for (Expression e : arrayAllocationExpression.dimensions) if (e != null) e.traverse(this, scope); context.popArgument(array); if (arrayAllocationExpression.initializer != null && arrayAllocationExpression.initializer.expressions != null) { for (Expression e : arrayAllocationExpression.initializer.expressions) e.traverse(this, scope); } return false; }
From source file:spoon.support.builder.JDTTreeBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w . ja v a2 s .co m public boolean visit(MessageSend messageSend, BlockScope scope) { CtInvocation<?> inv = factory.Core().createInvocation(); if (messageSend.binding != null) inv.setExecutable(references.getExecutableReference(messageSend.binding)); // inv // .setType(references // .getTypeReference(messageSend.binding.returnType)); context.enter(inv, messageSend); if (!(messageSend.receiver.getClass().equals(ThisReference.class))) messageSend.receiver.traverse(this, scope); context.pushArgument(inv); if (messageSend.arguments != null) for (Expression e : messageSend.arguments) { e.traverse(this, scope); } if (messageSend.genericTypeArguments != null) inv.setGenericTypes(references.getBoundedTypesReferences(messageSend.genericTypeArguments)); context.popArgument(inv); return false; }
From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java
License:Open Source License
private <T extends CtConstructorCall<Object>> T buildCommonPartForCtNewClassAndCtConstructorCall( AllocationExpression allocationExpression, BlockScope scope, T constructorCall) { if (allocationExpression.type != null) { final TypeReference[][] typeArguments = allocationExpression.type.getTypeArguments(); // If typeArguments are null or empty, we have an element with a generic type. if (typeArguments != null && typeArguments.length > 0) { context.isGenericTypeExplicit = true; // This loop is necessary because it is the only way to know if the generic type // is implicit or not. for (TypeReference[] typeArgument : typeArguments) { context.isGenericTypeExplicit = typeArgument != null && typeArgument.length > 0; if (context.isGenericTypeExplicit) { break; }// w ww . j a v a 2s .com } } constructorCall.setType(references.getTypeReference(allocationExpression.type.resolvedType)); context.isGenericTypeExplicit = true; } else if (allocationExpression.expectedType() != null) { constructorCall.setType(references.getTypeReference(allocationExpression.expectedType())); } constructorCall.setExecutable(references.getExecutableReference(allocationExpression.binding)); if (constructorCall.getExecutable() != null) { constructorCall.getExecutable() .setType((CtTypeReference<Object>) constructorCall.getExecutable().getDeclaringType()); } if (allocationExpression.genericTypeArguments() != null) { constructorCall.setActualTypeArguments( references.getBoundedTypesReferences(allocationExpression.genericTypeArguments())); } context.enter(constructorCall, allocationExpression); if (allocationExpression.enclosingInstance() != null) { context.target.push(constructorCall); allocationExpression.enclosingInstance().traverse(this, scope); context.target.pop(); } context.pushArgument(constructorCall); if (allocationExpression.arguments != null) { for (Expression e : allocationExpression.arguments) { e.traverse(this, scope); } } context.popArgument(constructorCall); return constructorCall; }
From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java
License:Open Source License
@Override public boolean visit(ArrayAllocationExpression arrayAllocationExpression, BlockScope scope) { CtNewArray<Object> array = factory.Core().createNewArray(); array.setType(references.getTypeReference(arrayAllocationExpression.resolvedType)); context.enter(array, arrayAllocationExpression); context.pushArgument(array);//from w w w . ja v a2s . com if (arrayAllocationExpression.dimensions != null) { for (Expression e : arrayAllocationExpression.dimensions) { if (e != null) { e.traverse(this, scope); } } } context.popArgument(array); if (arrayAllocationExpression.initializer != null && arrayAllocationExpression.initializer.expressions != null) { for (Expression e : arrayAllocationExpression.initializer.expressions) { e.traverse(this, scope); } } return false; }
From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java
License:Open Source License
@Override public boolean visit(MessageSend messageSend, BlockScope scope) { if (messageSend.actualReceiverType == null || !messageSend.actualReceiverType.isAnnotationType()) { CtInvocation<Object> inv = factory.Core().createInvocation(); if (messageSend.binding != null) { inv.setExecutable(references.getExecutableReference(messageSend.binding)); if (messageSend.binding instanceof ProblemMethodBinding) { // We are in a static complex in noclasspath mode. if (inv.getExecutable() != null && inv.getExecutable().getDeclaringType() != null) { final CtTypeAccess ta = factory.Core().createTypeAccess(); ta.setType(inv.getExecutable().getDeclaringType()); inv.setTarget(ta);/*from ww w . ja v a 2s. c o m*/ } if (messageSend.expectedType() != null) { inv.getExecutable().setType(references.getTypeReference(messageSend.expectedType())); } } } else { CtExecutableReference<Object> ref = factory.Core().createExecutableReference(); ref.setSimpleName(new String(messageSend.selector)); ref.setType(references.getTypeReference(messageSend.expectedType())); if (messageSend.receiver.resolvedType == null) { // It is crisis dude! static context, we don't have much more information. if (messageSend.receiver instanceof SingleNameReference || messageSend.receiver instanceof QualifiedNameReference) { final CtTypeReference<Object> typeReference = factory.Core().createTypeReference(); typeReference.setSimpleName(messageSend.receiver.toString()); ref.setDeclaringType(typeReference); } } else { ref.setDeclaringType(references.getTypeReference(messageSend.receiver.resolvedType)); } if (messageSend.arguments != null) { final List<CtTypeReference<?>> parameters = new ArrayList<CtTypeReference<?>>(); for (Expression argument : messageSend.arguments) { parameters.add(references.getTypeReference(argument.resolvedType)); } ref.setParameters(parameters); } inv.setExecutable(ref); } // inv // .setType(references // .getTypeReference(messageSend.binding.returnType)); context.enter(inv, messageSend); if (!(messageSend.receiver.getClass().equals(ThisReference.class))) { messageSend.receiver.traverse(this, scope); } context.pushArgument(inv); if (messageSend.arguments != null) { for (Expression e : messageSend.arguments) { e.traverse(this, scope); } } if (messageSend.genericTypeArguments() != null) { for (TypeBinding typeBinding : messageSend.genericTypeArguments()) { inv.getExecutable().addActualTypeArgument(references.getTypeReference(typeBinding)); } } context.popArgument(inv); return false; } else { CtAnnotationFieldAccess<Object> acc = factory.Core().createAnnotationFieldAccess(); acc.setVariable(references.getVariableReference(messageSend.binding)); acc.setType(references.getTypeReference(messageSend.resolvedType)); context.enter(acc, messageSend); context.target.push(acc); messageSend.receiver.traverse(this, scope); context.target.pop(); return false; } }